package com.wyt.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyt.controller.util.Data;
import com.wyt.pojo.Book;
import com.wyt.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Controller
public class AdminController {
    @Autowired
    private BookService bookService;

    /**
     * 管理员页面
     * @return
     */
    @GetMapping("/admin")
    public String index(){
        return "admin/index.html";
    }

    /**
     * 管理员home页
     * @return
     */
    @GetMapping("/admin/home")
    public String home(){
        return "redirect:/admin/home.html";
    }

    /**
     * 管理书籍页面
     * @param v 暂不知用处
     * @return
     */
    @GetMapping("/admin/book")
    public String book(String v){
        return "redirect:/admin/book.html";
    }

    /**
     * 添加书籍页面
     * @param v 暂不知用处
     * @return
     */
    @GetMapping("/add_book")
    public String add_book(String v){
        return "redirect:/admin/add_book.html";
    }
    @GetMapping("/edit_book")
    public String edit_book(String v){
        return "redirect:/admin/edit_book.html";
    }

    /**
     * 查询所以书籍和定向查询
     * @param page 第几页
     * @param limit 一页多少数据
     * @param searchParams 定向查询的书籍信息
     * @return
     */
    @GetMapping("/getBookJson")
    @ResponseBody
    public Data getBookJson(Integer page, Integer limit,@RequestParam(required = false) String searchParams){
        Data data = new Data();
        Map map = (Map)JSON.parse(searchParams);
        IPage<Book> ipage = new Page(page,limit);
        if (map!=null){
            String ID = (String)map.get("ID");
            String name = (String)map.get("name");
            String publisher = (String)map.get("publisher");
            QueryWrapper<Book> wrapper = new QueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(ID),"ID",ID).like(StringUtils.isNotBlank(name),"name",name).like(StringUtils.isNotBlank(publisher),"publish",publisher);
            bookService.page(ipage, wrapper);
        }else {
            bookService.page(ipage, null);
        }
        long count = bookService.count();
        List<Book> bookList = ipage.getRecords();
        data.setData(bookList);
        data.setCode("0");
        data.setCount(count+"");
        return data;
    }

    /**
     * 批量删除
     * @param map
     * @return
     */
    @PostMapping("/index/admin/deleteAllBookJson")
    @ResponseBody
    public Data removeBatch(@RequestParam Map map){
        Data data = new Data();
        Set key = map.keySet();
        ArrayList list = new ArrayList<>();
        for(Object o:key){
            list.add(map.get(o));
        }
        ArrayList arrayList = new ArrayList<>();
        for(int i=0;i<= list.size()-1;i=i+5){
            Object ID = list.get(i);
            arrayList.add(ID);
        }
        boolean flag = bookService.removeBatchByIds(arrayList);
        if (flag){
            data.setCode("1");
            data.setMessage("删除成功");
        }else {
            data.setCode("0");
            data.setMessage("删除失败");
        }

        return data;

    }

    /**
     * 删除一本图书
     * @param book 书籍信息
     * @return
     */
    @PostMapping("/index/admin/deleteBookJson")
    @ResponseBody
    public Data delete(Book book){
        boolean flag = bookService.removeById(book);
        Data data = new Data();
        if (flag){
            data.setCode("1");
            data.setMessage("删除成功！");
        }else {
            data.setCode("0");
            data.setMessage("删除失败！");

        }
        return data;
    }

    /**
     * 添加书籍信息
     * @return book 书籍信息
     */
    @PostMapping("/index/admin/addBookJson")
    @ResponseBody
    public Data addBookJson(Book book){
        book.setBorrowedNum(0);
        boolean save = bookService.save(book);
        Data data = new Data();
        if (save){
            data.setCode("1");
            data.setMessage("添加成功");
        }else {
            data.setCode("0");
            data.setMessage("添加失败");
        }
        return data;
    }

    /**
     * 修改图书信息
     * @param book 书籍信息
     * @return
     */
    @PostMapping("/index/admin/editBookJson")
    @ResponseBody
    public Data editBookJson(Book book){
        String ID = book.getId();
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(ID),"ID",ID);
        boolean update = bookService.update(book, wrapper);
        Data data = new Data();
        if (update){
            data.setCode("1");
            data.setMessage("更新成功");
        }else {
            data.setCode("0");
            data.setMessage("更新失败");
        }
        return data;
    }
}
